package com.bootdo.acitvity.DDD.domain.entity;

import lombok.Getter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;

public class ChannelLockedStock extends Stock{

    /**
     * 锁定效期
     */
    @Getter
    private Date flockStockEndTime;

    @Getter
    private Long channelId;

    private ChannelLockedStock(Builder builder) {
        super(builder.stockAmount);
        this.channelId = channelId;
        this.flockStockEndTime = builder.flockStockEndTime;
    }

    public static Builder getBuilder() {
        return new Builder();
    }

    public static Builder getBuilder(ChannelLockedStock lockedStock) {
        return new Builder(lockedStock);
    }

    public static class Builder {
        private Long stockAmount;
        private Date flockStockEndTime;
        private boolean isInit = false;

        public Builder isInit() {
            this.isInit = true;
            return this;
        }

        public Builder() {
        }

        public Builder(ChannelLockedStock lockedStock) {
            stockAmount = lockedStock.getStockAmount();
            flockStockEndTime = lockedStock.flockStockEndTime;
        }

        public Builder setStockAmount(Long stockAmount) {
            this.stockAmount = stockAmount;
            return this;
        }


        public Builder setFlockStockEndTime(Date flockStockEndTime) {
            this.flockStockEndTime = flockStockEndTime;
            return this;
        }

        private void validateParameters() {
            if (!isInit) {
                LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now().minusDays(1), LocalTime.MAX);
                Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
            }
        }

        public ChannelLockedStock build() {
            validateParameters();
            return new ChannelLockedStock(this);
        }
    }

    /**
     * 增加锁定库存
     *
     * @param stock
     * @return
     */
    @Override
    public ChannelLockedStock add(Stock stock) {
        return getBuilder(this).setStockAmount(this.getStockAmount() + stock.getStockAmount()).build();
    }

    /**
     * 扣减锁定库存
     *
     * @param stock
     * @return
     */
    @Override
    public ChannelLockedStock reduce(Stock stock) {
        return getBuilder(this).setStockAmount(this.getStockAmount() - stock.getStockAmount()).build();
    }

    /**
     * 扣减所有库存
     *
     * @return
     */
    public ChannelLockedStock reduceAll() {
        return getBuilder(this).setStockAmount(0L).build();
    }

}
